<template>
  <el-form
    ref="deptForm"
    :model="form"
    :rules="rules"
    label-width="120px"
  >
    <el-form-item label="部门名称" prop="name">
      <el-input v-model="form.name" style="width:80%" placeholder="1-50个字符" />
    </el-form-item>
    <el-form-item label="部门编码" prop="code">
      <el-input v-model="form.code" style="width:80%" placeholder="1-50个字符" />
    </el-form-item>
    <el-form-item label="部门负责人" prop="manager">
      <el-select v-model="form.manager" style="width:80%" placeholder="请选择">
        <el-option
          v-for="item in list"
          :key="item.id"
          :value="item.id"
          :label="item.username"
        />
      </el-select>
    </el-form-item>
    <el-form-item label="部门介绍" prop="introduce">
      <el-input v-model="form.introduce" style="width:80%" placeholder="1-300个字符" type="textarea" :rows="3" />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" size="small" @click="hSubmit">确定</el-button>
      <el-button size="small" @click="hCancel">取消</el-button>
    </el-form-item>
  </el-form>
</template>
<script>
import { getEmployeeSimple } from '@/api/employees'
import { addDepartments, updateDepartments, getDepartmentsQuery } from '@/api/departments'

export default {
  props: {
    id: {
      type: String,
      required: true
    },
    isEdit: {
      type: Boolean,
      required: true
    },
    originList: {
      type: Array,
      required: true
    }
  },
  data() {
    const validCode = (rule, value, callback) => {
      // 如果当时输入的部门在项目中的数据this.originList已经存在，提示用户从新输入
      let exsitCodeList = this.originList.map(item => item.code)
      console.log(exsitCodeList)
      // 判断是编辑状态，需要吧当前的部门的编码从已经存在的数组中移除
      if (this.isEdit) {
        exsitCodeList = this.originList.filter(item => item.id !== this.id).map(item => item.code)
      }
      if (exsitCodeList.includes(value)) {
        callback(new Error(`该部门编号：${value}已存在重新输入`))
      } else {
        callback()
      }
    }
    // 校验部门在统计部门中不能重复
    const validName = (rule, value, callback) => {
      // 如果当时输入的部门在项目中的数据this.originList已经存在，提示用户从新输入
      let exsitNameList = this.originList.filter(item => item.pid === this.id).map(item => item.name)
      console.log(exsitNameList)
      if (this.isEdit) {
        // 获取当前的pia
        const pid = this.originList.find(item => item.id === this.id).pid
        console.log('我是pid', pid)
        // 获取被占用的名称列表
        exsitNameList = this.originList.filter(item => item.pid === pid).filter(item => item.id !== this.id).map(item => item.name)
      }

      if (exsitNameList.includes(value)) {
        callback(new Error(`该部门编号：${value}已存在重新输入`))
      } else {
        callback()
      }
    }

    return {
      list: [],
      form: {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门负责人
        introduce: '' // 部门介绍
      },
      rules: {
        name: [
          { required: true, message: '请输入部门名称', trigger: 'blur' },
          { min: 1, max: 15, message: '请输入1到15个字符串', trigger: 'blur' },
          { validator: validName, trigger: 'blur' }
        ], // 部门名称
        code: [
          { required: true, message: '请输入部门编码', trigger: 'blur' },
          { min: 1, max: 15, message: '请输入1到15个字符串', trigger: 'blur' },
          { validator: validCode, trigger: 'blur' }
        ], // 部门编码
        manager: [
          { required: true, message: '请输入部门负责人', trigger: 'blur' }

        ], // 部门负责人
        introduce: [
          { required: true, message: '请输入部门介绍', trigger: 'blur' },
          { min: 1, max: 300, message: '请输入1到300个字符串', trigger: 'blur' }
        ] // 部门介绍
      }

    }
  },
  created() {
    this.loadEmployee()
    // 判断是否是编辑状态
    if (this.isEdit) {
      this.loadDeparmentDetail()
    }
  },
  methods: {
    // 重新表单
    resetForm() {
      this.$refs.deptForm.resetFields()
    },
    // 获取id中的编辑信息
    async loadDeparmentDetail() {
      try {
        const res = await getDepartmentsQuery(this.id)
        console.log('id查询', res)
        if (res.code !== 10000) return this.$message.error(res.message)
        this.$message.success(res.message)
        this.form = res.data
        console.log(res)
      } catch (error) {
        console.log(error)
      }
    },
    // 新增子部门
    async  doAdd() {
      try {
        const res = await addDepartments({ pia: this.id, ...this.form })
        console.log('将id添加进去', res)
        if (res.code !== 10000) return this.$message.error(res.message)
        this.$message.success(res.message)
        // 从新渲染
        this.$emit('success')
        // 关闭弹窗
      } catch (error) {
        console.log(error)
      }
    },
    // 根据id修改部门
    async updata() {
      try {
        const res = await updateDepartments(this.form)
        if (res.code !== 10000) return this.$message.error(res.message)
        this.$message.success(res.message)
        // 重新渲染页面
        // 关闭弹层
        this.$emit('success')
        console.log('根据id修改', res)
      } catch (error) {
        console.log(error)
      }
    },
    // 获取信息
    async loadEmployee() {
      try {
        const res = await getEmployeeSimple()
        console.log('用户信息', res)
        this.list = res.data
      } catch (error) {
        console.log(error)
      }
    },
    hSubmit() {
      // 兜底校验
      this.$refs.deptForm.validate((valid) => {
        if (!valid) return
        this.isEdit ? this.updata() : this.doAdd()
      })
    },
    hCancel() {
      // 点击关闭弹层
      this.$emit('close')
    }
  }

}
</script>
<style>

</style>
